/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Action;

import ActionForm.BuscarMesaActionForm;
import BE.Cliente;
import BE.Mesa;
import BL.ClienteBL;
import BL.MesaBL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.actions.LookupDispatchAction;

/**
 *
 * @author Xatuuu
 */
public class BuscarMesaAction extends LookupDispatchAction {

    /* forward name="success" path="" */
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward buscarMesa(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {

        BuscarMesaActionForm BuscarMesaActionForm = (BuscarMesaActionForm) form;

        Cliente objCliente = new Cliente();
        String strcodigoCliente = BuscarMesaActionForm.getStrCodigoCliente();
        int intcodigoCliente = 0;
        if (strcodigoCliente!="") {
            intcodigoCliente = Integer.parseInt(strcodigoCliente);
        }

        objCliente.setSrlIdpersona(intcodigoCliente);

        Mesa objMesa = new Mesa();
        String strcodigoMesa = BuscarMesaActionForm.getStrCodigoMesa();
        int intcodigoMesa = 0;
        if (strcodigoMesa!="") {
            intcodigoMesa = Integer.parseInt(strcodigoMesa);
        }
        objMesa.setSrlIdmesa(intcodigoMesa);
        objMesa.setCvNombre(BuscarMesaActionForm.getStrNombreMesa());

        objCliente.setMesa(objMesa);
        ArrayList<Cliente> arrClientesxMesa=new ArrayList<Cliente>();

        ClienteBL objClienteBL = new ClienteBL();
        if ((BuscarMesaActionForm.getStrCodigoCliente()=="")&&(BuscarMesaActionForm.getStrNombreCliente()==""))
        {
            if ((BuscarMesaActionForm.getStrCodigoMesa()!="")||(BuscarMesaActionForm.getStrNombreMesa()!=""))
            {
                    Mesa ObjMesaAux=new Mesa();
                    MesaBL objMesaBL=new MesaBL();
                    ObjMesaAux.setSrlIdmesa(intcodigoMesa);
                    ObjMesaAux.setCvNombre(BuscarMesaActionForm.getStrNombreMesa());
                    ArrayList<Mesa> arrMesa= (ArrayList<Mesa>) objMesaBL.buscarMesaenMesa(ObjMesaAux);


                for (Iterator<Mesa> it = arrMesa.iterator(); it.hasNext();) {
                Mesa mesa = it.next();
                Cliente auxCliente=new Cliente();
                auxCliente.setMesa(mesa);
                arrClientesxMesa.add(auxCliente);
            }
            }
            else
                 arrClientesxMesa = (ArrayList<Cliente>) objClienteBL.buscarMesaenCliente(objCliente);
        }
        else
        {
                arrClientesxMesa = (ArrayList<Cliente>) objClienteBL.buscarMesaenCliente(objCliente);

        }


        HttpSession session = request.getSession();
        session.setAttribute("arrClientesxMesa", arrClientesxMesa);
        BuscarMesaActionForm.setArrClientesxMesa(arrClientesxMesa);

        return mapping.findForward("BuscarMesa");

    }

    public ActionForward limpiarMesa(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {

        BuscarMesaActionForm buscarMesaActionForm = (BuscarMesaActionForm) form;


        return mapping.findForward("BuscarMesa");

    }

    @Override
    protected Map getKeyMethodMap() {
        Map map = new HashMap();
        map.put("button.BuscarMesa", "buscarMesa");
        map.put("button.BuscarCliente", "limpiarMesa");
        return map;
    }
}
